﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BillingSystem
{
    /// <summary>Диалог редактирования телефонного номера</summary>
    public partial class FormEditTelNumber : Form, IDialog
    {
        private decimal client_id;
        private BindingSource tariff;

        public FormEditTelNumber(BindingSource tariff, decimal client_id)
        {
            InitializeComponent();
            this.client_id = client_id;
            this.tariff = tariff;
        }

        public bool ShowForm(BindingSource bs)
        {
            DataRowView drv = (DataRowView)bs.Current;

            this.textBoxNum.DataBindings.Add("Text", drv, "NUM", false,
                DataSourceUpdateMode.OnValidation, null, "N0");
            this.textBoxAddress.DataBindings.Add("Text", drv, "ADDRESS", false,
                DataSourceUpdateMode.OnValidation, null);

            comboBoxTariff.DataSource = tariff;
            comboBoxTariff.ValueMember = "TARIFF_ID";
            comboBoxTariff.DisplayMember = "NAME";
            comboBoxTariff.DataBindings.Add("SelectedValue", drv, "TARIFF_ID", false, DataSourceUpdateMode.OnValidation);

            if (drv.IsNew)
            {
                drv["NUM"] = 0;
                drv["NUMBER_ID"] = 0;
                drv["ADDRESS"] = "";
                //drv["TARIFF_ID"] = "1";
                drv["CLIENT_ID"] = client_id;
                this.Text = "Создание нового номера";
                //textBoxNum.Text = "";
            }
            else
            {
                //tariff.Position = tariff.Find("TARIFF_ID", drv["TARIFF_ID"]);

                this.Text = "Редактирование номера";
            }



            if (this.ShowDialog() == DialogResult.OK)
            {
                if (textBoxNum.Text == null)
                {
                    drv.CancelEdit();
                    return false;
                }
                //drv["TARIFF_ID"] = ((DataRowView)tariff.Current)["TARIFF_ID"];
                drv.EndEdit();
                return true;
            }
            else
            {
                drv.CancelEdit();
                return false;
            }
        }
    }
}
